﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Dao;

namespace Web.Page
{
    public partial class ZL_CP_Query : BasePage
    {
        ZL_CP_Dao dao = new ZL_CP_Dao();
        protected void Page_Load(object sender, EventArgs e)
        {
            base.Page_Load(sender, e);
            if (!IsPostBack)
            {
                this.lbTitle.Text = "产品列表";
                initValue();
                BindGridview();
            }
        }

        private void initValue()
        {
            // 产品线
            CS_CPXSet_Dao daoCPX = new CS_CPXSet_Dao();
            List<CS_CPX> lsCPX = daoCPX.QueryAll();
            this.ddlCPXBH.DataSource = lsCPX;
            this.ddlCPXBH.DataTextField = "MC";
            this.ddlCPXBH.DataValueField = "BH";
            this.ddlCPXBH.DataBind();
            this.ddlCPXBH.Items.Insert(0, new ListItem("请选择", "0"));

            // 产品类型
            CS_CPLXSet_Dao daoCPLX = new CS_CPLXSet_Dao();
            List<CS_CPLX> lsCPLX = daoCPLX.QueryAll();
            this.ddlCPLXBH.DataSource = lsCPLX;
            this.ddlCPLXBH.DataTextField = "MC";
            this.ddlCPLXBH.DataValueField = "BH";
            this.ddlCPLXBH.DataBind();
            this.ddlCPLXBH.Items.Insert(0, new ListItem("请选择", "0"));
        }

        protected void GridView1_PageIndexChanged(object sender, EventArgs e)
        {
            BindGridview();
        }

        protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            GridView1.PageIndex = e.NewPageIndex;
            BindGridview();
            SavePageIndex(e.NewPageIndex);
        }

        private void BindGridview()
        {
            GridView1.PageIndex = RestorePageIndex();
            List<vCP> list = query(); // dao.QueryAll();
            this.GridView1.DataSource = list;
            this.GridView1.DataBind();
        }

        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            //if (e.Row.RowType == DataControlRowType.DataRow)
            //{
            //    ((ImageButton)(e.Row.Cells[9].Controls[0])).Attributes.Add("onclick ", "return  confirm( '确定删除？ ') ");
            //}
        }

        protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            string id = GridView1.DataKeys[e.RowIndex].Value.ToString();
            int result = dao.Delete(id);
            if (result == 1)
            {
                lbResult.Text = "删除成功";
                BindGridview();
            }
        }

        protected void btnAdd_Click(object sender, ImageClickEventArgs e)
        {
            Response.Redirect("~/Page/ZL_CP_Add.aspx");
        }

        protected void ddlCPXBH_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.ddlCPMCBH.Items.Clear();
            this.ddlCPXLBH.Items.Clear();
            this.ddlCPXHBH.Items.Clear();

            string cpx = this.ddlCPXBH.SelectedValue;
            if (!cpx.Equals("0"))
            {
                // 产品名称
                CS_CPMCSet_Dao daoCPMC = new CS_CPMCSet_Dao();
                List<CS_CPMC> lsCPMC = daoCPMC.QueryByCPX(cpx);
                this.ddlCPMCBH.DataSource = lsCPMC;
                this.ddlCPMCBH.DataTextField = "MC";
                this.ddlCPMCBH.DataValueField = "BH";
                this.ddlCPMCBH.DataBind();

                // 产品系列
                CS_CPXLSet_Dao daoCPXL = new CS_CPXLSet_Dao();
                List<CS_CPXL> lsCPXL = daoCPXL.QueryByCPX(cpx);
                this.ddlCPXLBH.DataSource = lsCPXL;
                this.ddlCPXLBH.DataTextField = "MC";
                this.ddlCPXLBH.DataValueField = "BH";
                this.ddlCPXLBH.DataBind();
            }

            this.ddlCPMCBH.Items.Insert(0, new ListItem("请选择", "0"));
            this.ddlCPXLBH.Items.Insert(0, new ListItem("请选择", "0"));
        }

        protected void ddlCPMCBH_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.ddlCPXHBH.Items.Clear();

            string cpx = this.ddlCPXBH.SelectedValue;
            string cpmc = this.ddlCPMCBH.SelectedValue;
            if (!cpmc.Equals("0"))
            {
                // 更新产品型号
                CS_CPXHSet_Dao daoCPXH = new CS_CPXHSet_Dao();
                List<CS_CPXH> lsCPXH = daoCPXH.QueryBy(cpx, cpmc);
                this.ddlCPXHBH.DataSource = lsCPXH;
                this.ddlCPXHBH.DataTextField = "MC";
                this.ddlCPXHBH.DataValueField = "BH";
                this.ddlCPXHBH.DataBind();

            }
            this.ddlCPXHBH.Items.Insert(0, new ListItem("请选择", "0"));
        }

        protected void btnQuery_Click(object sender, ImageClickEventArgs e)
        {
            BindGridview();
        }

        protected List<vCP> query()
        {
            string bh = this.tbBH.Text;
            string cpx = ddlCPXBH.SelectedValue;
            string cplx = ddlCPLXBH.SelectedValue;
            string cpmc = ddlCPMCBH.SelectedValue;
            string cpxl = ddlCPXLBH.SelectedValue;
            string cpxh = ddlCPXHBH.SelectedValue;

            return dao.QueryBySql(bh, cpx, cplx, cpmc, cpxh, cpxl);
        }
    }
}